﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;
using xClient.Framework.StaticResource;

namespace cMES1008
{
    public partial class frmKPNPrefix : Form
    {
        private DataGridViewRow _dgvRow = null;
        private string _strCmd = "ADD";
        public frmKPNPrefix(string vCMD, object param = null)
        {
            InitializeComponent();
            this._strCmd = vCMD;
            if (_strCmd.ToUpper().Equals("ADD"))
            {
                this.ClearFormInputData();
                this.Text = "新增料头信息";
            }
            else if (_strCmd.ToUpper().Equals("EDIT"))
            {
                this.Text = "修改料头信息";
                this.lblId.Visible = true;
                this.txtId.Visible = true;
                this.txtId.Enabled = false;

                if (param != null)
                {
                    this._dgvRow = (DataGridViewRow)param;
                }
                this.InitialViewData();

                this.txtUpdateUser.Text = StaticDictionary.Dictionary.UserID;
                this.dtpUpdateTime.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            }
        }

        private void InitialViewData()
        {
            if(this._dgvRow != null)
            {
                this.txtId.Visible = true;
                this.lblId.Visible = true;
                this.txtId.Text = _dgvRow.Cells[0].Value.ToString();
                this.txtKPNPrefix.Text = _dgvRow.Cells[1].Value.ToString();
                this.txtCustCode.Text = _dgvRow.Cells[2].Value.ToString();                
                this.txtRemarks.Text = _dgvRow.Cells[3].Value.ToString();
                this.txtIsValid.Text = _dgvRow.Cells[4].Value.ToString();
                this.txtCreateUser.Text = _dgvRow.Cells[6].Value.ToString();
                this.dtpCreateTime.text = _dgvRow.Cells[5].Value.ToString();
                this.txtUpdateUser.Text = _dgvRow.Cells[8].Value.ToString();
                this.dtpUpdateTime.text = _dgvRow.Cells[7].Value.ToString();
            }
        }

        private void ClearFormInputData()
        {
            this.lblId.Visible = false;
            this.txtId.Visible = false;
            this.txtCustCode.Text = "";
            this.txtKPNPrefix.Text = "";
            this.txtRemarks.Text = "";
            this.txtId.Text = "";
            this.txtRemarks.Text = "";
            this.txtCreateUser.Text = StaticDictionary.Dictionary.UserID;
            this.txtUpdateUser.Text = "";
            this.dtpCreateTime.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            this.txtCustCode.Focus();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtKPNPrefix.Text.Trim()))
            {
                MessageBox.Show("料头信息不能为空");
                return;
            }

            var _kpnDic = new Dictionary<string, object>();
            _kpnDic.Add("id", this.txtId.Text.Trim());
            _kpnDic.Add("cust_code", this.txtCustCode.Text.Trim().ToUpper());
            _kpnDic.Add("kpn_prefix", this.txtKPNPrefix.Text.Trim().ToUpper());
            _kpnDic.Add("remarks", this.txtRemarks.Text.Trim());
            _kpnDic.Add("create_user", this.txtCreateUser.Text.Trim());
            if (this._strCmd.Equals("ADD"))
            {
                _kpnDic.Add("create_time", this.dtpCreateTime.Text);
            }
            else if (this._strCmd.Equals("EDIT"))
            {
                _kpnDic.Add("update_user", this.txtCreateUser.Text.Trim());
                _kpnDic.Add("update_time", this.dtpUpdateTime.Text);
                _kpnDic.Add("is_valid", this.txtIsValid.Text);
            }

            var dataModel = JsonConvert.SerializeObject(_kpnDic);
            ExecutionResult<dynamic> result = new ExecutionResult<dynamic>();
            if (this._strCmd.Equals("ADD"))
            {
                result = Services.ServiceMethod.AddKPNPrefix(dataModel);
            }
            else if(this._strCmd.Equals("EDIT"))
            {
               result  = Services.ServiceMethod.UpdateKPNPrefix(dataModel);
            }
            
            if (result.Status)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(result.Message, "系统消息");
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}
